package com.zq.qq2;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.zq.qq2.service.FloatBackService;
import com.zq.qq2.service.FloatHomeService;
import com.zq.qq2.service.FloatQQService;
import com.zq.qq2.service.FloatWeixinService;

import java.io.File;


public class MainActivity extends Activity {

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            startService(new Intent(MainActivity.this, FloatQQService.class));
            startService(new Intent(MainActivity.this, FloatHomeService.class));
            //startService(new Intent(MainActivity.this, FloatFriendService.class));
            startService(new Intent(MainActivity.this, FloatBackService.class));
            startService(new Intent(MainActivity.this, FloatWeixinService.class));

            try {
                new File("/mnt/sdcard/zhangqing/").mkdir();
                new File("/mnt/sdcard/zhangqing/qq").mkdir();
                new File("/mnt/sdcard/zhangqing/weixin").mkdir();
                new File("/mnt/sdcard/zhangqing/src").mkdir();
                new File("/mnt/sdcard/zhangqing/config").mkdir();
                new File("/mnt/sdcard/zhangqing/data").mkdir();
                new File("/mnt/sdcard/zhangqing/ciku").mkdir();
            } catch (Exception e) {

            }
        }
    };

    Button moveXY;
    Button sure;
    CheckBox qq;
    CheckBox home;
    CheckBox back;
    CheckBox weixin;

    Button dama;
    TextView damaStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Constant.getSP(this);

        handler.sendEmptyMessage(0);
        qq = (CheckBox) findViewById(R.id.qq);
        home = (CheckBox) findViewById(R.id.home);
        back = (CheckBox) findViewById(R.id.back);
        weixin = (CheckBox) findViewById(R.id.weixin);

        qq.setChecked(Constant.getSP(this).getBoolean("qqshow", true));
        home.setChecked(Constant.getSP(this).getBoolean("homeshow", true));
        back.setChecked(Constant.getSP(this).getBoolean("backshow", true));
        weixin.setChecked(Constant.getSP(this).getBoolean("weixinshow", true));

        findViewById(R.id.user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, UserActivity.class));
            }
        });
        moveXY = (Button) findViewById(R.id.moveXY);
        moveXY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constant.getSP(MainActivity.this).edit().putBoolean("move", true).commit();

                stopService(new Intent(MainActivity.this, FloatQQService.class));
                startService(new Intent(MainActivity.this, FloatQQService.class));
                stopService(new Intent(MainActivity.this, FloatHomeService.class));
                startService(new Intent(MainActivity.this, FloatHomeService.class));

                //stopService(new Intent(MainActivity.this, FloatFriendService.class));
                //startService(new Intent(MainActivity.this, FloatFriendService.class));
                stopService(new Intent(MainActivity.this, FloatBackService.class));
                startService(new Intent(MainActivity.this, FloatBackService.class));

                stopService(new Intent(MainActivity.this, FloatWeixinService.class));
                startService(new Intent(MainActivity.this, FloatWeixinService.class));


            }
        });

        qq.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent(MainActivity.this, FloatQQService.class);
                if (isChecked)
                    startService(intent);
                else
                    stopService(intent);
                Constant.getSP(MainActivity.this).edit().putBoolean("qqshow", isChecked).commit();
            }
        });
        home.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent(MainActivity.this, FloatHomeService.class);
                if (isChecked)
                    startService(intent);
                else
                    stopService(intent);
                Constant.getSP(MainActivity.this).edit().putBoolean("homeshow", isChecked).commit();
            }
        });

        back.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent(MainActivity.this, FloatBackService.class);
                if (isChecked)
                    startService(intent);
                else
                    stopService(intent);
                Constant.getSP(MainActivity.this).edit().putBoolean("backshow", isChecked).commit();
            }
        });

        weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Intent intent = new Intent(MainActivity.this, FloatWeixinService.class);
                if (isChecked)
                    startService(intent);
                else
                    stopService(intent);
                Constant.getSP(MainActivity.this).edit().putBoolean("weixinshow", isChecked).commit();
            }
        });


        sure = (Button) findViewById(R.id.sure);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sure.setClickable(false);
                Constant.getSP(MainActivity.this).edit().putBoolean("move", false).commit();
                MainActivity.this.finish();

            }
        });

        dama = (Button) findViewById(R.id.dama);
        dama.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(0);
            }
        });

        Constant.Debug(Constant.getDeviceId(this));
        damaStatus = (TextView) findViewById(R.id.damastatus);
        showMoney();
    }

    Handler UiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            damaStatus.setText(msg.obj.toString());
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
        String[] cs = new String[]{"联众打码", "云码打码"};
        final SharedPreferences sp = Constant.getSP(this);
        builder.setSingleChoiceItems(cs, sp.getInt("dama", 0), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sp.edit().putInt("dama", which).commit();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                showMoney();
            }
        });
        return builder.create();
    }

    void showMoney() {
        new Thread() {
            @Override
            public void run() {
                StringBuilder sb = new StringBuilder();
                sb.append("平台:");
                if (Constant.getSP(MainActivity.this).getInt("dama", 0) == 0) {
                    sb.append("联众");
                    sb.append(",账号:");
                    sb.append(SharedPreferenceUtil.getInstance().getLianZongUser());
                    //sb.append(",www.jsdati.com");
                    sb.append(", " + CodeUtils.Money(0));
                } else {
                    sb.append("云码");
                    sb.append(",账号:");
                    sb.append(SharedPreferenceUtil.getInstance().getYunmaUser());
                    //sb.append(",www.yundama.com");
                    sb.append(", " + CodeUtils.Money(1));
                }
                Message msg = new Message();
                msg.obj = sb.toString();
                UiHandler.sendMessage(msg);
            }
        }.start();
    }
}
